package cn.hesion.leetcode;
/**
 * ClassName: ValidateStackSequences <br/>
 * Description: <br/>
 * date: 2022/6/14 5:47<br/>
 *
 * @author Hesion<br />
 * @version
 * @since JDK 1.8
 */

import io.netty.util.internal.StringUtil;

import java.util.Stack;

/**
 *
 * @author: hesion
 * @create: 2022-06-14 05:47
 **/
public class ValidateStackSequences {
    public boolean validateStackSequences(int[] pushed, int[] popped) {
        int i=0,j=0;
        Stack<Integer> stack = new Stack<Integer>();

        while(i<pushed.length||j<popped.length){
            if(i<pushed.length){
                stack.push(pushed[i++]);
            }
            while(!stack.empty()&&stack.peek() == popped[j]){
                stack.pop();
                ++j;
            }

            if(i==pushed.length){
                if (j<popped.length) {
                    return false;
                }
            }
        }
        return true;
    }
}
